<template>
  <div class="mod-distribution-user">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      v-model="dataForm"
      :table-loading="dataListLoading"
      :option="tableOption"
      @sort-change="sortChange"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @on-load="getDataList"
    >
      <template slot-scope="scope" slot="parentName">
        <div v-if="scope.row.parentDistributionUser === null">无</div>
        <div v-else>{{ scope.row.parentDistributionUser.nickName }}</div>
      </template>
      <template slot-scope="scope" slot="parentUserMobile">
        <div v-if="scope.row.parentDistributionUser === null">无</div>
        <div v-else>{{ scope.row.parentDistributionUser.userMobile }}</div>
      </template>
      <template slot-scope="scope" slot="boundCustomers">{{
        scope.row.distributionUserAchievementDataDto.boundCustomers
      }}</template>

      <template slot-scope="scope" slot="invitedVeeker">{{
        scope.row.distributionUserAchievementDataDto.invitedVeeker
      }}</template>

      <template slot-scope="scope" slot="addupAmount">{{
        scope.row.distributionUserAchievementDataDto.distributionUserWallet
          .addupAmount
      }}</template>

      <template slot-scope="scope" slot="menu">
        <el-button
          type="success"
          size="small"
          icon="el-icon-view"
          @click="info(scope.row)"
          v-if="isAuth('distribution:distributionUser:info')"
          >{{ $t("seckill.view") }}</el-button
        >

        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit"
          v-if="isAuth('distribution:distributionUser:update')"
          @click="addOrUpdateHandle(scope.row)"
          >{{ $t("distributionMsg.updateStatus") }}</el-button
        >
      </template>
    </avue-crud>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>

    <!-- 弹窗, 新增 / 修改 -->
    <Info v-if="infoVisible" ref="info"></Info>
  </div>
</template>
<script>
import { tableOption } from '@/crud/distribution/user/distributionUser'
import AddOrUpdate from './distribution-user-update'
import Info from './distribution-user-info'
export default {
  data () {
    return {
      dataForm: {
        // 分销员手机号
        mobile: null,
        // 邀请人手机号
        parentMobile: null,
        // 加入时间区间
        dateRange: []
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      searchForm: {},
      dataList: [],
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false,
      infoVisible: false,
      sortParam: 0, // 0无 1加入时间 2累计客户 3累计邀请 4累计收益
      sortType: 0 // 0无 1 正序 2倒序
    }
  },
  components: {
    AddOrUpdate,
    Info
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle (data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data)
      })
    },
    // 新增 / 修改
    info (data) {
      this.infoVisible = true
      this.$nextTick(() => {
        this.$refs.info.init(data)
      })
    },
    // 点击查询
    searchChange (params) {
      this.page.currentPage = 1

      this.searchForm = params
      this.getDataList(this.page)
    },
    // 刷新回调
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 获取数据列表
    getDataList (page) {
      this.$http({
        url: this.$http.adornUrl('/distribution/distributionUser/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page ? page.currentPage : 1,
          size: page ? page.pageSize : 20,
          sortParam: this.sortParam,
          sortType: this.sortType
        }, this.searchForm))
      }).then(({ data }) => {
        this.page.total = data.total
        this.dataList = data.records
        this.dataListLoading = false
      })
    },
    sortChange (data) {
      // 排序字段 0无 1加入时间 2累计客户 3累计邀请 4累计收益
      switch (data.prop) {
        case 'bindTime': this.sortParam = 1
          break
        case 'boundCustomers': this.sortParam = 2
          break
        case 'invitedVeeker': this.sortParam = 3
          break
        case 'addupAmount': this.sortParam = 4
          break
        default: this.sortParam = 0
          break
      }
      // 排序类型 0无 1 正序 2倒序
      switch (data.order) {
        case 'descending': this.sortType = 2
          break
        case 'ascending': this.sortType = 1
          break
        default: this.sortType = 0
          break
      }
      this.getDataList(this.page)
    }
  }
}
</script>
<style scoped>
div /deep/ .avue-crud__menu{
    overflow:inherit;
    min-height: 0;
}
div /deep/ .avue-crud__right{
    position: absolute;
    right: 0;
    top: -58px;
}
div /deep/ .avue-crud__pagination{
  margin-bottom: 25px;
}
</style>
